<template>
	<view class="box">
		<view class="top_box ding">
			<view class="left">下单门店</view>
			<view class="right">{{shopname ? shopname :'门店名称'}}</view>
		</view>
		<view class="top_box">
			<view class="box1 ding">
				<view class="left">下单产品</view>
				<view class="right" @click="openselect">
					{{productname ? productname : '请选择'}}
					<uni-icons type="right"></uni-icons>
				</view>
			</view>
			<view class="box1 ding">
				<view class="left">下单数量</view>
				<view class="right"><input type="number" v-model="params.productCount" placeholder="请输入数量" /></view>
			</view>
		</view>
		<view class="top_box">
			<view class="box1 ding">
				<view class="left">收货人</view>
				<view class="right"><input v-model="params.deliveryRecipientName" type="text" placeholder="请输入收货人姓名" />
				</view>
			</view>
			<view class="box1 ding">
				<view class="left">收货电话</view>
				<view class="right">
					<input v-model="params.deliveryRecipientPhone" type="number" maxlength="11" placeholder="请输入电话" />
			</view>
			</view>
			<view class="box1 ding">
				<view class="left">所在地区</view>
				<view class="right" @click="address">{{addressl ? addressl :'请选择省市区'}}<uni-icons
						type="right"></uni-icons></view>
			</view>
			<liu-customize-sel :address="ADS" class="tanchudizhi" ref="scroll"
				@change='chooseSuccess'></liu-customize-sel>
			<view class="box1 ding">
				<view class="left">详细地址</view>
				<view class="right"><input type="text" v-model="params.deliveryAddress" placeholder="请输入详细地址" /></view>
			</view>
		</view>
		<view class="btns">
			<view class="dibu">
				<span>合计：￥{{price}}</span>
				<button @click="queren">确认下单</button>
			</view>
		</view>
		<uni-popup ref="popup" type="bottom" :safe-area='false'>
			<view class="tanchu">
				<view class="top">
					<view class="wenzi">
						<span style="margin-right: -10%;">选择产品</span>
					</view>
					<uni-icons @click="close(1)" type="closeempty" size="20"></uni-icons>
				</view>
				<view class="hezi" v-for="(item,index) in dizhilist" :key="index" @click="xuanzedizhi(item,index)">
					<view class="left">
						<image :src="indexs == index ? '../../static/ddimg/d3.png' : '../../static/ddimg/d2.png'"
							mode="">
						</image>
						<view>
							<span>{{item.productName}}</span>
							<p>{{item.productPriceShow}}</p>
						</view>
					</view>
				</view>
			</view>
		</uni-popup>
		<uni-popup type="center" ref="order">
			<view class="orders">
				<uni-icons @click="close(2)" type="closeempty" size="20" class="icon"></uni-icons>
				<view class="title">
					{{title}}
				</view>
				<view class="btnsflex">
					<view class="btnl b" @click="close(2)">取消</view>
					<view class="btnr b" @click="duck()">确认</view>
				</view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	import {
		getproduct,
		adddeliverys
	} from '@/common/http.api.js';
	export default {
		data() {
			return {
				title: '是否确认下单',
				params: {
					shopId: '',
					productId: '',
					productCount: '',
					deliveryRecipientName: '',
					deliveryRecipientPhone: '',
					deliveryProvince: '',
					deliveryCity: '',
					deliveryCounty: '',
					deliveryAddress: '',
				},
				shopname: '',
				productname: '',
				productprice: '',
				indexs: -1,
				dizhilist: [],
				addressl: '',
				ADS: ''
			}
		},
		computed: {
			price() {
				return this.params.productCount * this.productprice
			}
		},
		onLoad(option) {
			const datas = JSON.parse(decodeURIComponent(option.item))
			console.log(datas);
			this.params.shopId = datas.id
			this.shopname = datas.shopName
			getproduct({}).then(res => {
				this.dizhilist = res.rows
			})
		},
		methods: {
			queren() {
				 if(this.params.productId == '') {
					uni.showToast({
						icon: 'error',
						title: '选择下单产品'
					})
				} else if(this.params.productCount == '') {
					uni.showToast({
						icon: 'error',
						title: '请输入下单数量'
					})

				}else if(this.params.deliveryRecipientName == '') {
					uni.showToast({
						icon: 'error',
						title: '收货人不能为空'
					})

				}else if(this.params.deliveryRecipientPhone == '') {
					uni.showToast({
						icon: 'error',
						title: '电话不能为空'
					})

				}else if(!/^1(3[0-9]|4[01456879]|5[0-35-9]|6[2567]|7[0-8]|8[0-9]|9[0-35-9])\d{8}$/.test(this.params
						.deliveryRecipientPhone)) {
					uni.showToast({
						icon: 'error',
						title: '手机号格式不对'
					})

				}else if(this.addressl == '') {
					uni.showToast({
						icon: 'error',
						title: '请选择地址'
					})

				}else if(this.params.deliveryAddress == '') {
					uni.showToast({
						icon: 'error',
						title: '请输入详细地址'
					})
				}else{
					this.$refs.order.open()
					
				}
			},
			duck() {
				adddeliverys(this.params).then(res => {
					this.$refs.order.close()
					uni.navigateTo({
						url: '/pages/dier/xiadanjilu'
					})
				})
			},

			xuanzedizhi(item, index) {
				this.indexs = index
				console.log(item);
				this.params.productId = item.id
				this.productname = item.productName
				this.productprice = item.productPriceShow
				this.$refs.popup.close()
			},
			openselect() {
				this.$refs.popup.open()
			},
			address() {
				this.$refs.scroll.open()
			},
			close(i) {
				if (i == 1) {
					this.$refs.popup.close()
				} else {
					this.$refs.order.close()
				}
			},
			//地址选择成功
			chooseSuccess(e) {
				if (e.value == 0) {
					this.addressl = ''
					this.ADS = ''
					this.params.deliveryProvince = ''
					this.params.deliveryCity = ''
					this.params.deliveryCounty = ''
				} else {
					this.addressl = e.value[0].label + e.value[1].label + e.value[2].label
					this.ADS = e.value[2].label
					this.params.deliveryProvince = e.value[0].label
					this.params.deliveryCity = e.value[1].label
					this.params.deliveryCounty = e.value[2].label
				}
			},
		}
	}
</script>

<style lang="scss" scoped>
	.orders {
		width: 449rpx;
		height: 300rpx;
		background: #FFFFFF;
		border-radius: 30rpx;
		padding: 30rpx;

		.icon {
			position: absolute;
			right: 30rpx;
		}

		.title {
			margin-top: 80rpx;
			font-family: PingFang SC;
			font-weight: bold;
			font-size: 32rpx;
			color: #0A0A0A;
			line-height: 36rpx;
			text-align: center;
		}

		.btnsflex {
			margin-top: 120rpx;
			display: flex;
			justify-content: space-between;
			align-items: center;
			text-align: center;
			line-height: 58rpx;

			.b {
				width: 177rpx;
				height: 58rpx;
				border-radius: 29rpx;
				font-size: 28rpx;
				font-weight: bold;
			}

			.btnl {
				color: #302215;
				background: linear-gradient(42deg, #F5E1BD 0%, #F2D099 100%);
			}

			.btnr {
				color: #E4C085;
				background: linear-gradient(42deg, #5A4629 0%, #21150A 53%, #302315 100%);
			}
		}
	}

	.btns {
		width: 90%;
		position: fixed;
		bottom: 40rpx;
		padding-bottom: constant(safe-area-inset-bottom);
		padding-bottom: env(safe-area-inset-bottom);
	}

	.dibu {
		height: 100rpx;
		background: linear-gradient(84deg, #24180D 0%, #594629 100%);
		border-radius: 43rpx;
		display: flex;
		justify-content: space-between;
		align-items: center;

		span {
			font-family: PingFang SC;
			font-weight: 500;
			font-size: 28rpx;
			color: #FFEEDB;
			line-height: 33rpx;
			margin-left: 20rpx;
		}

		button {
			width: 217rpx;
			height: 74rpx;
			background: linear-gradient(84deg, #F2D09A 0%, #F5E0BD 100%);
			border-radius: 37rpx;
			font-family: PingFang SC;
			font-weight: 800;
			font-size: 30rpx;
			color: #3A2B18;
			line-height: 33rpx;
			margin: 0;
			margin-right: 6rpx;
		}
	}

	input {
		font-size: 28rpx;
		text-align: right;
	}

	.ding {
		display: flex;
		align-items: center;
		justify-content: space-between;
	}

	.top_box {
		width: 650rpx;
		background: #FFFFFF;
		border-radius: 20rpx;
		padding: 20rpx;
		margin-top: 20rpx;

		.left {
			font-family: PingFang SC;
			font-weight: 500;
			font-size: 28rpx;
			color: #0A0A0A;
		}

		.right {
			font-size: 28rpx;
			font-weight: 500;
		}

		.box1 {
			height: 100rpx;
		}
	}





	.tanchu {
		background-color: #FFFFFF;
		border-radius: 30rpx 30rpx 0rpx 0rpx;
		padding-bottom: constant(safe-area-inset-bottom);
		padding-bottom: env(safe-area-inset-bottom);

		button {
			width: 677rpx;
			height: 80rpx;
			background: linear-gradient(84deg, #24180D 0%, #594629 100%);
			border-radius: 40rpx;
			font-family: Source Han Sans CN;
			font-weight: 400;
			font-size: 28rpx;
			color: #FFFFFF;
			line-height: 33rpx;
			margin-top: 100rpx;
		}

		.hezi {
			display: flex;
			align-items: center;
			justify-content: space-between;
			margin-top: 20rpx;
			padding-bottom: 20rpx;
			border-bottom: 1rpx solid #EEEEEE;

			.left {
				display: flex;
				align-items: center;

				span {
					font-family: PingFang SC;
					font-weight: 500;
					font-size: 28rpx;
					color: #070707;
					line-height: 33rpx;
				}

				p {
					font-family: PingFang SC;
					font-weight: 500;
					font-size: 24rpx;
					line-height: 33rpx;
					margin-top: 10rpx;
				}

				image {
					margin-left: 25rpx;
					margin-right: 20rpx;
				}
			}

			.right {
				margin-right: 20rpx;
			}

			image {
				width: 35rpx;
				height: 35rpx;
			}
		}

		.top {
			width: 100%;
			border-bottom: 1px solid #EEEEEE;
			display: flex;
			align-items: center;

			.wenzi {
				width: 90%;
				height: 100rpx;
				text-align: center;
				font-family: Source Han Sans CN;
				font-weight: 400;
				font-size: 30rpx;
				color: #070707;
				line-height: 100rpx;
				// margin-left: -10%;
			}
		}
	}

	.tanchudizhi {
		position: absolute;
		bottom: 0;
		width: 100%;
		background: #ffff;
		left: 0;
		border-radius: 24px 24px 0 0;
	}
</style>